<template>
  <div class="" style="height: calc(100vh - 100px); overflow: auto">
    <el-scrollbar style="height: 100%">
      <div
        v-infinite-scroll="loadArticleList"
        infinite-scroll-disabled="disabled"
        infinite-scroll-immediate="true"
      >
        <el-row :gutter="10">
          <el-col class="hidden-md-and-down" :lg="3" :xl="5"
            ><div
              class="grid-content bg-purple"
              style="min-height: calc(100vh - 100px)"
            ></div
          ></el-col>

          <el-col :lg="18" :xl="14">
            <div
              class="grid-content bg-purple-light"
              style="min-height: calc(100vh - 100px); background: #e5e9f2"
            >
              <el-carousel indicator-position="outside" height="200px">
                <el-carousel-item v-for="item in 4" :key="item">
                  <h3>{{ item }}</h3>
                </el-carousel-item>
              </el-carousel>
              <div class="article-filter">
                <span class="mr24">排序：</span>
                <span
                  class="mr24 order-type"
                  v-bind:class="{ active: isPublishTime }"
                  @click="orderBy = 'createTime'"
                  >按发布时间</span
                >
                <span
                  class="mr24 order-type"
                  v-bind:class="{ active: isReadNum }"
                  @click="orderBy = 'readNum'"
                  >按访问量</span
                >
                <span>分类：</span>
                <el-select v-model="classify" clearable placeholder="请选择">
                  <el-option
                    v-for="item in classifys"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </div>
              <div class="infinite-list-wrapper">
                <ul class="note-list">
                  <ArticleListItem
                    v-for="item in articleList"
                    :key="item.id"
                    v-bind:item="item"
                  ></ArticleListItem>
                </ul>
                <p v-if="loading">加载中...</p>
                <p v-if="noMore">没有更多了</p>
              </div>
            </div>
          </el-col>

          <el-col class="hidden-md-and-down" :lg="3" :xl="5"
            ><div
              class="grid-content bg-purple"
              style="min-height: calc(100vh - 100px)"
            ></div
          ></el-col>
        </el-row>
      </div>
    </el-scrollbar>
    <el-backtop target=".el-scrollbar__wrap"></el-backtop>
  </div>
</template>

<script>
import ArticleListItem from "../components/ArticleListItem";
import { loadAllClassify } from "@/api/classifyService";
import _ from "lodash";
export default {
  components: {
    ArticleListItem,
  },
  data() {
    return {
      count: 10,
      pageNum: 0,
      loading: false,
      noMore: false,
      articleList: [],
      classifys: [],
      classify: "",
      orderBy: "createTime",
    };
  },
  computed: {
    disabled() {
      return this.loading || this.noMore;
    },
    isPublishTime() {
      return this.orderBy === "createTime";
    },
    isReadNum() {
      return this.orderBy === "readNum";
    },
  },
  watch: {
    orderBy: function () {
      this.articleList = [];
      this.noMore = false;
    },
    classify: function () {
      this.articleList = [];
      this.noMore = false;
    },
  },
  methods: {
    loadArticleList() {
      let _this = this;
      this.loading = true;
      let pageNum = this.pageNum;
      let orderBy = this.orderBy;
      let classifyId = this.classify;
      this.axios
        .get("/blogbackend/article/loadArticleList", {
          params: {
            classifyId: classifyId,
            pageNum: pageNum,
            orderBy: orderBy,
          },
        })
        .then(function (response) {
          console.log(response);
          let pageData = response.data.data;
          _this.noMore = pageData.last;
          _this.loading = false;
          _this.articleList = _.concat(_this.articleList, pageData.content);
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    async loadClassifys() {
      let result = await loadAllClassify();
      let datas = result.data.data;
      let options = [];
      _.each(datas, (item) => {
        options.push({
          value: item.id,
          label: item.name,
        });
      });
      this.classifys = options;
    },
  },
  mounted() {
    this.loadClassifys();
  },
};
</script>

<style>
.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.el-scrollbar__wrap {
  overflow-x: hidden !important;
}
.is-horizontal {
  display: none;
}

.note-list {
  margin: 0;
  padding: 0;
  list-style: none;
  text-align: left;
}

.article-filter {
  height: 40px;
  background-color: #d3d3d366;
  text-align: left;
  line-height: 40px;
  display: flex;
  justify-content: flex-end;
}

.article-filter .mr24 {
  margin-right: 24px;
  color: #999aaa;
}

.article-filter .order-type {
  cursor: pointer;
}

.article-filter .active {
  color: #323f48;
}
.article-filter input {
  background-color: #d3d3d30f;
}
</style>
